<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button
          type="primary"
          icon="plus"
          v-if="save"
          @click="handleAdd">新建</a-button>
      </div>
      <div>
        <a-spin :spinning="confirmLoading">
          <s-table
            ref="table"
            size="default"
            rowKey="projectTypeId"
            :columns="columns"
            :data="loadData"
            showPagination="auto"
            :customRow="tableCustomRow">
            <span
              slot="serial"
              slot-scope="text, record, index">
              {{ index + 1 }}
            </span>
            <span
              slot="createTime"
              slot-scope="text, record">
              {{ record.createTime.split(' ')[0] }}
            </span>
            <span
              slot="action"
              slot-scope="text, record">
              <template>
                <a v-if="update" @click="handleEdit(record)">配置</a>
                <a-divider v-if="update" type="vertical" />
                <a v-if="deleteBtn" @click="() => deleteProduct(record)">
                  删除
                </a>
              </template>
            </span>
          </s-table>
        </a-spin>
      </div>
      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :isAdd="isAdd"
        @cancel="handleCancel"
        @ok="handleOk" />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getProjectTypeList, saveProjectType, updateProjectType, delProjectType } from '@/api/manage'

import CreateForm from './modules/CreateForm'
import { message, Modal } from 'ant-design-vue'
import { mapGetters } from 'vuex'
import { isInMenu } from '../config'

const columns = [
  {
    title: '#',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '创建日期',
    dataIndex: 'createTime',
    scopedSlots: { customRender: 'createTime' }
  },
  {
    title: '项目类型',
    dataIndex: 'projectType'
  },
  {
    title: '备注',
    dataIndex: 'note'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm
  },
  data () {
    this.columns = columns
    return {
      // QUANXIAN
      save: false,
      list: false,
      update: false,
      deleteBtn: false,
      visible: false,
      confirmLoading: false,
      queryParam: {},
      isAdd: false,
      loadData: parameter => {
        const requestParameters = { page: parameter.pageNo, limit: parameter.pageSize }
        const param = Object.assign({}, this.queryParam)
        return this.list && getProjectTypeList(Object.assign({}, param, requestParameters)).then(res => {
          return Object.assign({}, res, {
            totalCount: res.total,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize
          })
        })
      },
      projectType: undefined
    }
  },
  created () {
    // 实际采购
    this.save = isInMenu(this.menu, 'ROLE_but:projectType:save')
    this.list = isInMenu(this.menu, 'ROLE_but:projectType:list') && isInMenu(this.menu, 'ROLE_but:projectType:info')
    this.update = isInMenu(this.menu, 'ROLE_but:projectType:update')
    this.deleteBtn = isInMenu(this.menu, 'ROLE_but:projectType:delete')
  },
  computed: {
    ...mapGetters(['menu'])
  },
  methods: {
    deleteProduct (record) {
      Modal.confirm({
        title: '删除项目类型',
        content: '删除后数据不再展示，需要查看请联系开发',
        onOk: () => {
          this.confirmLoading = true
          delProjectType({ projectTypeId: record.projectTypeId })
            .then(() => {
              this.$refs.table.refresh()
              message.success('删除成功')
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleAdd () {
      this.mdl = null
      this.visible = true
      this.isAdd = true
    },
    handleEdit (record) {
      this.visible = true
      this.isAdd = false
      this.product = record
      this.confirmLoading = false
      const form = this.$refs.createModal.form
      this.$nextTick(() => {
        form.setFieldsValue(record)
      })
    },
    handleOk () {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          if (this.isAdd) {
            saveProjectType(values)
              .then(res => {
                if (res.code === 0) {
                  this.visible = false
                  this.$refs.table.refresh()
                  this.$message.info('新增成功')
                  form.resetFields()
                }
              })
              .finally(() => {
                this.confirmLoading = false
              })
          } else {
            updateProjectType(Object.assign({}, values, { projectTypeId: this.projectType.projectTypeId }))
              .then(res => {
                if (res.code === 0) {
                  this.visible = false
                  this.$refs.table.refresh()
                  this.$message.info('配置成功')
                  form.resetFields()
                  // this.customLoading = false
                }
              })
              .finally(() => {
                this.confirmLoading = false
              })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    tableCustomRow (record, index) {
      return {
        // style: {
        //   'background-color': record.id === this.physicalSurveyCurrRowId ? '#1890FF' : 'white'
        // },
        on: {
          click: event => {
            this.projectType = record
          }
        }
      }
    }
  }
}
</script>
